﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace SS
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {
            GetRand();
        }
        HttpClient httpClient = new HttpClient();
        VoteUrl voteUrl = null;
        VoteCount voteCount = new VoteCount();
        private void Form2_Load(object sender, EventArgs e)
        {
            voteUrl = new VoteUrl(adressVoteShow: "http://www.heixiaotu.net/index.asp",

                  adressVoteSend: "http://www.heixiaotu.net/checkuserlogin.asp",
                  adressCode: "http://www.heixiaotu.net/GetCode.asp");
            httpClient = new HttpClient(new Uri(voteUrl.AdressVoteShow));
            byte[] responseData = httpClient.DownloadData(voteUrl.AdressVoteShow);
            httpClient.Encoding = Encoding.GetEncoding("gb2312");
            string srcString = httpClient.DownloadString(voteUrl.AdressVoteShow);
            GetRand();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 1; i++)
            {
                Thread t = new Thread(new ThreadStart(NewMethod));
                t.Start();
            }
        }

        private void NewMethod()
        {
            //HttpClient hc = new HttpClient(new Uri(vu.voteUrl.AdressVoteShow));
            while (true)
            {
                try
                {
                    string postData = "username=feiazi&userpassword=11111&linkaddress2=&verifycode=" +
                        this.textBox1.Text.Trim()
                    + "&imageField.x=28&imageField.y=3";
                    //<script LANGUAGE='javascript'>alert('请输入正确的验证码！');history.go(-1);window.location.href='index.asp'</script>
                    //<script LANGUAGE='javascript'>alert('对不起！您的用户名或密码有误！');history.go(-1);window.location.href='index.asp'</script>

                    string s = httpClient.PostData(voteUrl.AdressVoteSend,
                        postData, "gb2312", "gb2312");
                    if (s.Contains("您的用户名或密码有误"))
                    {
                        this.SetText("您的用户名或密码有误");
                    }
                    else if (s.Contains("请输入正确的验证码"))
                    {
                        this.SetText("请输入正确的验证码");
                    }
                    else
                    {
                        this.SetText("密码正确");
                        break;
                    }
                }
                catch (Exception ee)
                {
                }
            }
        }
        //给值
        /// <summary>
        ///  给值
        /// </summary>
        /// <param name="text"></param>
        private void SetText(string text)
        {
            if (this.rtbResult.InvokeRequired)
            {
                SetTextCallback set = new SetTextCallback(SetText);
                this.Invoke(set, text);
            }
            else
            {
                this.rtbResult.AppendText(string.Format("{0}.{1} {2}{3}", voteCount.NumberCount++, DateTime.Now, text,Environment.NewLine));
                this.rtbResult.ScrollToCaret();
                if (voteCount.NumberCount % 2000 == 0)
                {
                    //GetNum();

                }
                if (voteCount.NumberCount % 4000 == 0)
                {
                    //Log();
                }
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            GetRand();
        }

        private void GetRand()
        {
            if (File.Exists("szz.gif"))
            {
                File.Delete("szz.gif");
            }
            httpClient.DownloadFile(new Uri(voteUrl.AdressCode), "szz.gif");
            FileInfo fi = new FileInfo("szz.gif");
            if (fi != null)
            {
                fi.Attributes = FileAttributes.Hidden;
            }
            pictureBox1.LoadAsync("szz.gif");
        }

        
    }
}
